package com.handu.luna.core.domain.support;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUploadSupport {
	
	public enum ImageType{
		AFFICHEIMG("/afficheimg/"),BRANDS("/brands/"),BRANDGENERALINFO("/brandGeneralInfo/"),ABLUM("/ablum/"),
		ATTR("/attr/"),CONPIC("/conpic/"),CONPIC_OTHERS("/conpic_others/")
        ,GOODS("/goods/"),ARTICLES("/articles/"),ADVERTISES("/advertises/"),GOODSCOLOR("/goodscolor/"),TOPIC("/topic")
        ,SALESPROMOTION("/salespromotion/"),SECKILLGROUPBUY("/seckillgroupbuy/"),TOPICIMAGE("/topicimage/")
        ,COMBO("/combo/");
		
		public String path;
		
		private ImageType(String path){
			this.path = path;
		}
	}
	
	public enum FileType{
		XLS("/temp/");
		
		public String path;
		
		private FileType(String path){
			this.path = path;
		}
	}
	/**
	 * 
	 * @param inputStream 上传的文件流
	 * @param filename 已写好的文件名称
	 * @param imageType 文件类型
	 * @return
	 * @throws IOException
	 */
	public static  String upload(InputStream inputStream,String filename , ImageType imageType) throws IOException{
		//设置存储路径
		String absolutPath;
		if(System.getenv().containsKey("UPLOAD_PATH")){
			absolutPath = System.getenv("UPLOAD_PATH")+"/images"+imageType.path
					+new SimpleDateFormat("yyyyMM").format(new Date());
		}else{
			absolutPath = "bin/static/images"+imageType.path
					+new SimpleDateFormat("yyyyMM").format(new Date());
		}
		
		//创建文件夹 若不存在
		if(!new File(absolutPath).exists()){
			new File(absolutPath).mkdirs();
		}
		
		//开始存储文件
		byte[] bytes = new byte[1024];
		
		BufferedInputStream in = new BufferedInputStream(inputStream);
		
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(absolutPath+"/"+filename)));
		
		while(in.read(bytes)!=-1){
			out.write(bytes);
		}
		
		out.close();
		
		in.close();
		
		
		return absolutPath.substring(absolutPath.indexOf("/images/"))+"/"+filename;
	}
	
	/**
	 * 
	 * @param inputStream 上传的文件流
	 * @param filename 已写好的文件名称
	 * @param imageType 文件类型
	 * @return
	 * @throws IOException
	 */
	public static  String uploadFile(InputStream inputStream,String filename , FileType fileType) throws IOException{
		//设置存储路径
		String absolutPath;
		if(System.getenv().containsKey("UPLOAD_PATH")){
			absolutPath = System.getenv("UPLOAD_PATH")+"/images"+fileType.path
					+new SimpleDateFormat("yyyyMM").format(new Date());
		}else{
			absolutPath = "bin/static/images"+fileType.path
					+new SimpleDateFormat("yyyyMM").format(new Date());
		}
		
		//创建文件夹 若不存在
		if(!new File(absolutPath).exists()){
			new File(absolutPath).mkdirs();
		}
		
		//开始存储文件
		byte[] bytes = new byte[1024];
		
		BufferedInputStream in = new BufferedInputStream(inputStream);
		
		BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(absolutPath+"/"+filename)));
		
		while(in.read(bytes)!=-1){
			out.write(bytes);
		}
		
		out.close();
		
		in.close();
		
		
		return absolutPath+"/"+filename;
	}
	
}
